package com.pb.wkflow.api.controller;

import com.pb.infra.oth.entity.common.Result;
import com.pb.wkflow.api.controller.request.ProcessTaskRequest;
import com.pb.wkflow.api.service.ProcTaskService;
import com.pb.wkflow.core.config.DeleteReasonConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程任务API
 * @author 苗益辉
 * @date 2022/9/1 11:20
 */
@Slf4j
@RestController
@RequestMapping("v1/workflow/procTask")
public class ProcTaskApi {

    @Autowired
    ProcTaskService procTaskService;

    /**
     * 查询待办列表
     * @param pageIndex 页码
     * @param pageSize 页数
     * @param request request
     * @return Result
     */
    @PostMapping("findTodoList")
    public Result findTodoList(@RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                               @RequestBody ProcessTaskRequest request) {
        log.info("---------- 查询待办列表 ---------- {}", request);
        return procTaskService.findToDoList(pageIndex, pageSize, request);
    }

    /**
     * 查询待办数量
     * @return Result
     */
    @GetMapping("queryTodoCount")
    public Result queryTodoCount() {
        log.info("---------- 查询待办数量 ---------- ");
        return procTaskService.queryTodoCount();
    }

    /**
     * 查询已办列表
     * @param pageIndex 页码
     * @param pageSize 页数
     * @param request request
     * @return Result
     */
    @PostMapping("findHasDoneList")
    public Result findHasDoneList(@RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
                                  @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                  @RequestBody ProcessTaskRequest request) {
        log.info("---------- 查询已办列表 ---------- {}", request);
        return procTaskService.findHasDoneList(pageIndex, pageSize, request);
    }

    /**
     * 查询我的申请列表
     * @param pageIndex 页码
     * @param pageSize 页数
     * @param request request
     * @return Result
     */
    @PostMapping("findMyApplyList")
    public Result findMyApplyList(@RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
                                  @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                  @RequestBody ProcessTaskRequest request) {
        log.info("---------- 查询我的申请列表 ---------- {}", request);
        return procTaskService.findMyApplyList(pageIndex, pageSize, request);
    }

    /**
     * 提交任务，审批通过
     * @param request request
     * @return Result
     */
    @PostMapping("submitTask")
    public Result submitTask(@RequestBody ProcessTaskRequest request) {
        log.info("---------- 提交任务 ---------- {}", request);
        return Result.ok(procTaskService.submitTask(request, DeleteReasonConstants.PROCESS)).setMessage("提交成功");
    }

    /**
     * 拒绝任务（结束流程）
     * @param request request
     * @return Result
     */
    @PostMapping("refuseTask")
    public Result refuseTask(@RequestBody ProcessTaskRequest request) {
        log.info("---------- 拒绝任务 ---------- {}", request);
        return Result.ok(procTaskService.refuseTask(request)).setMessage("拒绝成功");
    }

    /**
     * 终止流程/结束流程
     * @param request request
     * @return Result
     */
    @PostMapping("terminateTask")
    public Result terminateTask(@RequestBody ProcessTaskRequest request) {
        log.info("---------- 终止流程 ---------- {}", request);
        return Result.ok(procTaskService.terminateTask(request)).setMessage("终止成功");
    }

    /**
     * 退回到任务前一节点/撤回任务
     * @param request request
     * @return Result
     */
    @PostMapping("backPreTask")
    public Result backPreTask(@RequestBody ProcessTaskRequest request) {
        log.info("---------- 撤回任务 ---------- {}", request);
        return Result.ok(procTaskService.backPreTask(request)).setMessage("撤回成功");
    }

    /**
     * 回收任务
     * @param request request
     * @return Result
     */
    @PostMapping("reBackTask")
    public Result reBackTask(@RequestBody ProcessTaskRequest request) {
        log.info("---------- 回收任务 ---------- {}", request);
        return Result.ok(procTaskService.reBackTask(request)).setMessage("回收成功");
    }

    /**
     * 退回到首个用户节点/退回发起人
     * @param request request
     * @return Result
     */
    @PostMapping("backToFirstUserTask")
    public Result backToFirstUserTask(@RequestBody ProcessTaskRequest request) {
        log.info("---------- 退回发起人 ---------- {}", request);
        return Result.ok(procTaskService.backToFirstUserTask(request,DeleteReasonConstants.REBACK2FIRST)).setMessage("退回成功");
    }

    /**
     * 转派、调度任务
     * @param request request
     * @return Result
     */
    @PostMapping("transferTask")
    public Result transferTask(@RequestBody ProcessTaskRequest request) {
        log.info("---------- 转派、调度任务 ---------- {}", request);
        return Result.ok(procTaskService.transferTask(request)).setMessage("调度成功");
    }

    /**
     * 发起人提交，重新发起
     * @param request request
     * @return Result
     */
    @PostMapping("submitByStart")
    public Result submitByStart(@RequestBody ProcessTaskRequest request) {
        log.info("---------- 发起人提交，重新发起 ---------- {}", request);
        procTaskService.submitByStart(request);
        return Result.ok(procTaskService.submitTask(request, DeleteReasonConstants.START)).setMessage("发起成功");
    }

    /**
     * 发起人撤回
     * @param request request
     * @return Result
     */
    @PostMapping("reBackByStart")
    public Result reBackByStart(@RequestBody ProcessTaskRequest request) {
        log.info("---------- 发起人撤回 ---------- {}", request);
        return Result.ok(procTaskService.reBackByStart(request)).setMessage("撤回成功");
    }

    /**
     * 批量审批通过
     * @param requests requests
     * @return Result
     */
    @PostMapping("/batchSubmitTask")
    public Result batchSubmitTask(@RequestBody List<ProcessTaskRequest> requests) {
        log.info("---------- 批量审批通过 ---------- {}", requests);
        return procTaskService.batchSubmitTask(requests);
    }

    /**
     * 批量审批拒绝
     * @param requests requests
     * @return Result
     */
    @PostMapping("/batchRefuseTask")
    public Result batchRefuseTask(@RequestBody List<ProcessTaskRequest> requests) {
        log.info("---------- 批量审批拒绝 ---------- {}", requests);
        return procTaskService.batchRefuseTask(requests);
    }

    /**
     * 获取列表的各审批类型的数量
     * @param request request
     * @return Result
     */
    @PostMapping("/getApprovalTypeCount")
    public Result getApprovalTypeCount(@RequestBody ProcessTaskRequest request) {
        log.info("---------- 获取列表的各审批类型的数量 ---------- {}", request);
        return procTaskService.getApprovalTypeCount(request);

    }
}
